package com.jdy.haoduoai.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoai.activity.BaseActivity;
import com.jdy.haoduoai.model.CommentItem;
import com.jdy.haoduoai.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//import com.jdy.haoduoai.R;

/**
 * 用户评论列表
 * 
 * @author lzq
 * 
 */
@SuppressLint("InflateParams")
public class CommentAdapter extends BaseAdapter {

  private BaseActivity mContext;
  private LayoutInflater mInflater;
  private ArrayList<CommentItem> mList;
  private DisplayImageOptions options;

  public CommentAdapter(BaseActivity context, ArrayList<CommentItem> list) {
    this.mContext = context;
    this.mList = list;
    mInflater = LayoutInflater.from(mContext);

    // 用 displayImage 加载图片 的 配置参数
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.defaut_parent_avtar_female) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    // 创建默认的ImageLoader配置参数
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs() // 打印 log 信息
        .build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    CommentItem itemVo = mList.get(position);
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.commlistitem, null);
      holder.headpic = (ImageView) convertView.findViewById(R.id.headpic);
      holder.username = (TextView) convertView.findViewById(R.id.username);
      holder.comment = (TextView) convertView.findViewById(R.id.comment);
      holder.writetime = (TextView) convertView.findViewById(R.id.writetime);
      holder.replyryt = (RelativeLayout) convertView
          .findViewById(R.id.replyryt);
      holder.reply = (TextView) convertView.findViewById(R.id.reply);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (itemVo != null) {
      if (Tools.isNotEmpty(itemVo.member.avatar)) {
        ImageLoader.getInstance().displayImage(itemVo.member.avatar,
            holder.headpic, options);
      }

      holder.username.setText(itemVo.member.nickname);
      holder.comment.setText(itemVo.comment);
      holder.writetime.setText(Tools.changeTime(itemVo.created_at));
    }

    if (itemVo == null || TextUtils.isEmpty(itemVo.reply)) {
      holder.replyryt.setVisibility(View.GONE);
    } else {
      holder.replyryt.setVisibility(View.VISIBLE);
      holder.reply.setText(itemVo.reply);
    }
    return convertView;
  }

  private static class ViewHolder {
    public ImageView headpic;
    public TextView username;
    public TextView comment;
    public TextView writetime;
    public RelativeLayout replyryt; // 系统回复 的布局
    public TextView reply; // 系统的回复
  }

}
